<?php
class LoginAction extends Action{
	/*
	* 登陆页面
	* @return void
	*/
	public function index()
	{
		if(empty($_SESSION['userArr']['name'])){
                    $user = unserialize(stripslashes($_COOKIE['admin']));
                    $this->assign('user',$user);
                    $this->display();exit;
		}
		$this->redirect('/'.GROUP_NAME.'/Index/index');
	}

	/*
	* 验证码
	* @return void
	*/
	public function verify(){
		import("ORG.Util.Image");
		Image::buildImageVerify();
	}

	/*
	* 检查登陆
	* @params $_POST
	* @return void
	*/
	public function checkLogin(){
		$username = I('post.username');
		$password = I('post.password');
		//检查验证码
		if(empty($_COOKIE['admin'])){
			if(empty($_POST['verify'])){
				$this->error("验证码不能为空");
			}
			if($_SESSION['verify'] != md5($_POST['verify'])){
				$this->error("验证码错误！");
			}
		}
		//检查用户名、密码是否为空
		if(empty($_POST['username']) or empty($_POST['password'])){
			$this->error("账号或密码不能为空");
		}
		//检查是否有该用户
		$Form = M("user");
		$userArr = $Form->where("name='".$username."'")->find();
		if(!in_array($username, $userArr)){$this->error("没有该用户");}
		//判断密码是否是记住密码，如果32位则是记住密码
		if(strlen($password)==32){
			$pwd = $password;
		}else{
			$pwd = md5($password.$userArr['rand']);
		}
		//判断密码
		if($userArr['pwd'] != $pwd){$this->error("账号或密码错误!");}		
		//记录日志
		//rizhi($this->_post());
		//记录cookies,记住密码，默认记住10天
		if($_POST['jizhu']){
                    $u = serialize($userArr);
                    setcookie("admin", $u, time()+3600*24*10,'/');
		}
                //记录session
		unset($userArr['pwd']);
		$_SESSION['userArr']	= $userArr;
		$this->success("登陆成功",U("Admin/Index/index"));			
	}
	
	/**
	 * 注销登陆
	 * 
	 * @return void
	 */
	public function logout(){
		if (!isset ($_SESSION) )
		session_start();
		unset ($_SESSION['userArr']);
		session_unset(); 
		session_destroy();
		$this->error("请登陆后访问","__GROUP__/Login/index");
	}

	
}